/**
 * Initialize your data structure here.
 * @param {number} size
 */
var MovingAverage = function(size) {
  this.queue = [];
  this.now = 0;
  this.size = size;
};

/**
 * @param {number} val
 * @return {number}
 */
MovingAverage.prototype.next = function(val) {
  if(this.queue.length < this.size) {
    this.queue.push(val);
    this.now++;
  } else {
    this.queue[this.now % this.size] = val;
    this.now++;
  }
  let avg = 0;
  for(let i = 0; i < this.queue.length; i++) {
    avg += this.queue[i];
  }
  return avg / this.queue.length;
};

/**
 * Your MovingAverage object will be instantiated and called as such:
 * var obj = new MovingAverage(size)
 * var param_1 = obj.next(val)
 */
